package yt.kpool.activity.setting.agent.recommendcustomers;


import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.widget.RelativeLayout;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import java.util.ArrayList;
import butterknife.BindView;
import butterknife.ButterKnife;
import yt.kpool.R;
import yt.kpool.adapter.ExpandableRecyclerAdapter;
import yt.kpool.adapter.RecommendCustomersAdapter;
import yt.kpool.bean.CommentItem;
import yt.kpool.bean.RecommendCustomersBean;
import yt.kpool.network.HttpUtils;
import yt.kpool.network.MyGson;
import yt.kpool.network.NetHelperNew;
import yt.kpool.util.TitleControl;
import yt.kpool.util.ToastUtil;
import yt.kpool.view.dialog.LoadingDialog;

/**
 * 推荐客户
 */
public class RecommendCustomersAc extends AppCompatActivity {
    @BindView(R.id.list)
    LRecyclerView mRecyclerView;
    private RecommendCustomersAdapter mDataAdapter;
    private LRecyclerViewAdapter mLRecyclerViewAdapter = null;
    private LoadingDialog dialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_recomendcustomer);
        ButterKnife.bind(this);
        initTitle();
        initView();
        getNetData();

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 70);
            Log.i("-->", "读写权限申请");
        }
    }

    /**
     * 设置标题栏
     */
    private void initTitle() {
        TitleControl t = new TitleControl(this);
        t.setTitle("推荐客户");

    }

    private void initView() {
        dialog = new LoadingDialog(this, 0);
        mDataAdapter = new RecommendCustomersAdapter(RecommendCustomersAc.this, mRecyclerView);
        mDataAdapter.setMode(ExpandableRecyclerAdapter.MODE_ACCORDION);
        mLRecyclerViewAdapter = new LRecyclerViewAdapter(mDataAdapter);
        mRecyclerView.setAdapter(mLRecyclerViewAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mLRecyclerViewAdapter.addHeaderView(new RelativeLayout(this));
        mRecyclerView.setPullRefreshEnabled(false);
    }

    /**
     * 获取网络数据
     */
    private void getNetData() {
        dialog.show();
        NetHelperNew.PromotesList(new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                dialog.dismiss();
                RecommendCustomersBean recommendCustomersBean = MyGson.getInstance().fromJson(data, RecommendCustomersBean.class);
                if (recommendCustomersBean.getType() == 1) {
                    if (recommendCustomersBean.getData().size() > 0) {
                        ArrayList<CommentItem> list = new ArrayList<>();
                        for (int i = 0; i < recommendCustomersBean.getData().size(); i++) {
                            list.add(new CommentItem(recommendCustomersBean.getData().get(i).getProportionName(), recommendCustomersBean.getData().get(i).getCreatedTime()));
                            list.add(new CommentItem(recommendCustomersBean.getData().get(i).getCode(),recommendCustomersBean.getData().get(i).getProportionsDetails()));

                        }
                        mDataAdapter.setItems(list);
                        mLRecyclerViewAdapter.notifyDataSetChanged();
                    } else {
                        ToastUtil.showToast(RecommendCustomersAc.this, "没有可查询数据");
                    }
                } else {
                    ToastUtil.showToast(RecommendCustomersAc.this, recommendCustomersBean.getContent());
                }

            }

            @Override
            public void onError(String msg) {
                super.onError(msg);
                dialog.dismiss();
                ToastUtil.showToast(RecommendCustomersAc.this, msg);
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        doNext(requestCode, grantResults);
    }

    private void doNext(int requestCode, int[] grantResults) {
        if (requestCode == 70) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {


            } else {

                new AlertDialog.Builder(this)
                        .setTitle("权限申请")
                        .setMessage("在设置-应用-" + getString(R.string.app_name) + "-权限中启动读写权限，以正常使用保存图片功能")

                        .setNegativeButton("取消", null)
                        .show();
            }
        }

    }

}
